home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / book / Chap03 / Shapes / Shapes.c next >
Encoding:
C/C++ Source or Header  |  1999-09-22  |  5.8 KB  |  273 lines

  1. // Shapes.c
  2. // OpenGL SuperBible, Chapter 3
  3. // Demonstrates GLUT menuing and some GLUT prebuilt shapes
  4. // Program by Richard S. Wright Jr.
  5.  
  6. #include <windows.h>
  7. #include <gl/gl.h>
  8. #include <gl/glu.h>
  9. #include <gl/glut.h>
  10.  
  11.  
  12.  
  13. // Rotation amounts
  14. static GLfloat xRot = 0.0f;
  15. static GLfloat yRot = 0.0f;
  16.  
  17.  
  18. // Light values and coordinates
  19. GLfloat  ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
  20. GLfloat  diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  21. GLfloat  specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
  22. GLfloat  specref[] =  { 1.0f, 1.0f, 1.0f, 1.0f };
  23.  
  24. static int iShape = 1;
  25.  
  26. ///////////////////////////////////////////////////////////////////////////////
  27. // Reset flags as appropriate in response to menu selections
  28. void ProcessMenu(int value)
  29.     {
  30.     iShape = value;
  31.  
  32.     glutPostRedisplay();
  33.     }
  34.  
  35.  
  36. // Called to draw scene
  37. void RenderScene(void)
  38.     {
  39.     // Clear the window
  40.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  41.  
  42.  
  43.     // Save matrix state and do the rotation
  44.     glPushMatrix();
  45.     glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  46.     glRotatef(yRot, 0.0f, 1.0f, 0.0f);
  47.  
  48.     switch(iShape)
  49.         {
  50.         case 1:
  51.             glutWireSphere(1.0f, 25, 25);
  52.             break;
  53.  
  54.         case 2:
  55.             glutWireCube(1.0f);
  56.             break;
  57.  
  58.         case 3:
  59.             glutWireCone(0.30f, 1.1f, 20, 20);
  60.             break;
  61.  
  62.         case 4:
  63.             glutWireTorus(0.3f, 1.0f, 10, 25);
  64.             break;
  65.  
  66.         case 5:
  67.             glutWireDodecahedron();
  68.             break;
  69.  
  70.         case 6:
  71.             glutWireOctahedron();
  72.             break;
  73.  
  74.         case 7:
  75.             glutWireTetrahedron();
  76.             break;
  77.  
  78.         case 8:
  79.             glutWireIcosahedron();
  80.             break;
  81.  
  82.         case 9:
  83.             glutWireTeapot(1.0f);
  84.             break;
  85.  
  86.         case 11:
  87.             glutSolidSphere(1.0f, 25, 25);
  88.             break;
  89.  
  90.         case 12:
  91.             glutSolidCube(1.0f);
  92.             break;
  93.  
  94.         case 13:
  95.             glutSolidCone(0.30, 1.1f, 20, 20);
  96.             break;
  97.     
  98.         case 14:
  99.             glutSolidTorus(0.3f, 1.0f, 10, 25);
  100.             break;
  101.  
  102.         case 15:
  103.             glutSolidDodecahedron();
  104.             break;
  105.  
  106.         case 16:
  107.             glutSolidOctahedron();
  108.             break;
  109.  
  110.         case 17:
  111.             glutSolidTetrahedron();
  112.             break;
  113.  
  114.         case 18:
  115.             glutSolidIcosahedron();
  116.             break;
  117.  
  118.         default:
  119.             glutSolidTeapot(1.0f);
  120.             break;
  121.         }
  122.  
  123.  
  124.     // Restore transformations
  125.     glPopMatrix();
  126.  
  127.     // Flush drawing commands
  128.     glutSwapBuffers();
  129.     }
  130.  
  131. // This function does any needed initialization on the rendering
  132. // context. 
  133. void SetupRC()
  134.     {
  135.     // Black background
  136.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
  137.  
  138.     // Enable Depth Testing
  139.     glEnable(GL_DEPTH_TEST);
  140.  
  141.     // Enable lighting
  142.     glEnable(GL_LIGHTING);
  143.  
  144.     // Setup and enable light 0
  145.     glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
  146.     glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
  147.     glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
  148.     glEnable(GL_LIGHT0);
  149.  
  150.     // Enable color tracking
  151.     glEnable(GL_COLOR_MATERIAL);
  152.     
  153.     // Set Material properties to follow glColor values
  154.     glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
  155.  
  156.     // All materials hereafter have full specular reflectivity
  157.     // with a high shine
  158.     glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
  159.     glMateriali(GL_FRONT,GL_SHININESS,128);
  160.  
  161.  
  162.     // Set drawing color to blue
  163.     glColor3ub(0, 0, 255);
  164.     }
  165.  
  166. void SpecialKeys(int key, int x, int y)
  167.     {
  168.     if(key == GLUT_KEY_UP)
  169.         xRot-= 5.0f;
  170.  
  171.     if(key == GLUT_KEY_DOWN)
  172.         xRot += 5.0f;
  173.  
  174.     if(key == GLUT_KEY_LEFT)
  175.         yRot -= 5.0f;
  176.  
  177.     if(key == GLUT_KEY_RIGHT)
  178.         yRot += 5.0f;
  179.  
  180.     if(key > 356.0f)
  181.         xRot = 0.0f;
  182.  
  183.     if(key < -1.0f)
  184.         xRot = 355.0f;
  185.  
  186.     if(key > 356.0f)
  187.         yRot = 0.0f;
  188.  
  189.     if(key < -1.0f)
  190.         yRot = 355.0f;
  191.  
  192.     // Refresh the Window
  193.     glutPostRedisplay();
  194.     }
  195.  
  196.  
  197. void ChangeSize(int w, int h)
  198.     {
  199.     GLfloat     lightPos[] = { -50.f, 50.0f, 100.0f, 1.0f };
  200.     GLfloat nRange = 1.9f;
  201.  
  202.     // Prevent a divide by zero
  203.     if(h == 0)
  204.         h = 1;
  205.  
  206.     // Set Viewport to window dimensions
  207.     glViewport(0, 0, w, h);
  208.  
  209.     // Reset projection matrix stack
  210.     glMatrixMode(GL_PROJECTION);
  211.     glLoadIdentity();
  212.  
  213.     // Establish clipping volume (left, right, bottom, top, near, far)
  214.     if (w <= h) 
  215.         glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
  216.     else 
  217.         glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
  218.  
  219.     // Reset Model view matrix stack
  220.     glMatrixMode(GL_MODELVIEW);
  221.     glLoadIdentity();
  222.  
  223.     glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
  224.     }
  225.  
  226. int main(int argc, char* argv[])
  227.     {
  228.     int nSolidMenu;
  229.     int nWireMenu;
  230.     int nMainMenu;
  231.  
  232.     glutInit(&argc, argv);
  233.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  234.     glutCreateWindow("GLUT Shapes");
  235.     
  236.     // Create the Menu
  237.     nWireMenu = glutCreateMenu(ProcessMenu);
  238.     glutAddMenuEntry("Sphere",1);
  239.     glutAddMenuEntry("Cube",2);
  240.     glutAddMenuEntry("Cone",3);
  241.     glutAddMenuEntry("Torus",4);
  242.     glutAddMenuEntry("Dodecahedron",5);
  243.     glutAddMenuEntry("Octahedron",6);
  244.     glutAddMenuEntry("Tetrahedron",7);
  245.     glutAddMenuEntry("Icosahedron",8);
  246.     glutAddMenuEntry("Teapot",9);
  247.  
  248.  
  249.     nSolidMenu = glutCreateMenu(ProcessMenu);
  250.     glutAddMenuEntry("Sphere",11);
  251.     glutAddMenuEntry("Cube",12);
  252.     glutAddMenuEntry("Cone",13);
  253.     glutAddMenuEntry("Torus",14);
  254.     glutAddMenuEntry("Dodecahedron",15);
  255.     glutAddMenuEntry("Octahedron",16);
  256.     glutAddMenuEntry("Tetrahedron",17);
  257.     glutAddMenuEntry("Icosahedron",18);
  258.     glutAddMenuEntry("Teapot",19);
  259.  
  260.     nMainMenu = glutCreateMenu(ProcessMenu);
  261.     glutAddSubMenu("Wire", nWireMenu);
  262.        glutAddSubMenu("Solid", nSolidMenu);
  263.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  264.     
  265.     glutReshapeFunc(ChangeSize);
  266.     glutSpecialFunc(SpecialKeys);
  267.     glutDisplayFunc(RenderScene);
  268.     SetupRC();
  269.     glutMainLoop();
  270.  
  271.     return 0;
  272.     }
  273.